package com.sixmac.service.impl;

import com.sixmac.core.Constant;
import com.sixmac.dao.PlacardDao;
import com.sixmac.entity.Placard;
import com.sixmac.service.OperaService;
import com.sixmac.service.PlacardService;
import com.sixmac.utils.MessageTimerWork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/31 0031 下午 2:41.
 */
@Service
public class PlacardServiceImpl implements PlacardService {

    @Autowired
    private PlacardDao placardDao;

    @Autowired
    private OperaService operaService;

    @Override
    public List<Placard> findAll() {
        return placardDao.findAll();
    }

    @Override
    public Page<Placard> find(int pageNum, int pageSize) {
        return placardDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Placard> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Placard getById(int id) {
        return placardDao.findOne(id);
    }

    @Override
    public Placard deleteById(int id) {
        Placard placard = getById(id);
        placardDao.delete(placard);

        // 删除消息时，杀死线程
        MessageTimerWork.killWork("two" + id);

        return placard;
    }

    @Override
    @Transactional
    public void deleteById(HttpServletRequest request, Integer id) {
        Placard placard = getById(id);

        operaService.addOperaInfo(request, "删除公告 " + placard.getTitle());

        placardDao.delete(placard);

        // 删除消息时，杀死线程
        MessageTimerWork.killWork("two" + id);
    }

    @Override
    public Placard create(Placard placard) {
        return placardDao.save(placard);
    }

    @Override
    public Placard update(Placard placard) {
        return placardDao.save(placard);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<Placard> pageByParams(final String title, Integer pageNum, Integer pageSize) {
        Page<Placard> page = placardDao.findAll(new Specification<Placard>() {
            @Override
            public Predicate toPredicate(Root<Placard> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<Predicate>();

                if (null != title && title != "") {
                    Predicate pre = cb.like(root.get("title").as(String.class), "%" + title + "%");
                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.ASC, "id"));

        return page;
    }

    @Override
    @Transactional
    public void deleteAll(HttpServletRequest request, int[] ids) {
        for (int id : ids) {
            deleteById(request, id);
        }
    }
}